package me.zhengjie.minbearwcs.serivce.agv.task.agvTask.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.common.Result;
import me.zhengjie.common.ResultCodeEnum;
import me.zhengjie.controller.TestController;
import me.zhengjie.minbearwcs.entity.PO.agv.AgvContentPool;
import org.springframework.stereotype.Component;
import me.zhengjie.minbearwcs.dao.agv.inf.AgvMissionDao;
import me.zhengjie.minbearwcs.dao.agv.inf.AgvMissionDetailDao;
import me.zhengjie.minbearwcs.entity.DTO.agv.task.AgvActionSource;
import me.zhengjie.minbearwcs.entity.DTO.agv.task.AgvTaskSource;
import me.zhengjie.minbearwcs.entity.PO.agv.AgvMission;
import me.zhengjie.minbearwcs.entity.PO.agv.AgvMissionDetail;
import me.zhengjie.minbearwcs.entity.PO.agv.missionEnum.AgvMissionStatus;
import me.zhengjie.minbearwcs.serivce.agv.task.agvTask.AgvTaskDispatcher;
import me.zhengjie.minbearwcs.utils.IdWorker;
import me.zhengjie.minbearwcs.utils.WcsHpptUtil;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class AgvTaskDispatcherImpl implements AgvTaskDispatcher {
    
    private final IdWorker idWorker;
    private final AgvMissionDao agvMissionDao;
    private final AgvMissionDetailDao agvMissionDetailDao;
    private final WcsHpptUtil wcsHpptUtil;
    private final TestController agvDispatcher;
    
    @Override
    public boolean dispatch(AgvTaskSource agvTaskSource) {
        String taskId = "Task_Agv_" + idWorker.nextId();
        String targetCollect = agvTaskSource.getActionList()
                .stream()
                .map(AgvActionSource::getPoint)
                .collect(Collectors.joining(",", "[", "]"));

        AgvMission agvMission = new AgvMission(null, taskId, agvTaskSource.getMissionType(), AgvMissionStatus.READY,
                1, targetCollect, agvTaskSource.getActionList().size(), AgvContentPool.AGV_ACTION_INIT, null, agvTaskSource.getPalletCode(), agvTaskSource.getWcsMissionId(),
                agvTaskSource.getWcsMissionType(), agvTaskSource.getWcsMissionStatus(), LocalDateTime.now(),
                null, null);
        agvMissionDao.insert(agvMission);

        AtomicInteger actionIndexCount = new AtomicInteger(1);
        agvTaskSource.getActionList()
                .stream()
                .map(a-> new AgvMissionDetail(null,taskId, actionIndexCount.getAndIncrement(), a.getPoint(),
                            a.getAction().name(),null,0))
                .collect(Collectors.toList())
                .forEach(agvMissionDetailDao::insert);

//        List<String> pointList = agvTaskSource.getActionList()
//                .stream()
//                .map(AgvActionSource::getPoint)
//                .collect(Collectors.toList());

//        TUSKMission tuskMission = new TUSKMission(taskId, pointList, agvTaskSource.getMissionType().name());
//        TUSKResponse tuskResponse = tuskHpptUtil.dispatchTUSKMission(tuskMission);

        Result<Object> objectResult = agvDispatcher.sentTask(
                agvTaskSource.getActionList().get(0).getPoint(),
                agvTaskSource.getActionList().get(1).getPoint(),
                taskId,
                agvTaskSource.getMissionType().getTuskType()
        );


        return Objects.equals(objectResult.getCode(), ResultCodeEnum.SUCCESS.getCode());
    }
}
